/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.data.orm.jpa;

/**
 * @author wangyandong
 * @date 2019/9/5 13:02
 *
 */

import org.hibernate.EmptyInterceptor;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * hibernate拦截器：表名替换、国际化字段替换
 * 单例
 */
@SuppressWarnings({"JavaDoc", "unused"})
public class CAFHibernateInterceptor extends EmptyInterceptor {

    /**
     * 默认的拦截器构造函数
     */
    public CAFHibernateInterceptor() {

    }

    /**
     * sql执行前拦截器方法
     * @param sql
     * @return
     */
    @Override
    public String onPrepareStatement(String sql) {
        try(DynamicTableContext context = DynamicTableContext.getContext()){
            DynamticTableInfo info = context.getDynamicTableInfo();
            if(info ==null){
                return sql;
            }

            //动态表
            if(info.getDynamicTableNames()!=null) {
                Map<String, String> maps = info.getDynamicTableNames();
                for(Map.Entry<String,String> entry:maps.entrySet()){
                    String srcTableName = entry.getKey();
                    String destTableName = entry.getValue();
                    //仅当sql中不存在目标表时，才进行替换
                    if(Pattern.compile("(?i)\\s" + destTableName + "\\s").matcher(sql).find() == false) {
                        //如果存在 table1 table1 模式的表别名
                        if(Pattern.compile("(?i)\\s" + srcTableName + "\\s+" + srcTableName + "\\s").matcher(sql).find() == true) {
                            //正则表达式替换: 仅匹配表名前后带空格的情况k
                            sql = sql.replaceAll("(?i)\\s" + srcTableName + "\\s+" + srcTableName + "\\s",
                                    " " + destTableName + " " + srcTableName + " ");
                        }
//                        else if(Pattern.compile("(?i)from\\s.*\\s+" + srcTableName + "\\s" + "where").matcher(sql).find() == true){
//                            //如果出现from nottable1 table1 where 什么也不做
//                            //doNothing
//                            //from dfad table1 where
//                        }
                        else {
                            //正则表达式替换: 仅匹配表名前后带空格的情况
                            sql = sql.replaceAll("(?i)\\s" + srcTableName + "\\s", " " + destTableName + " ");
                        }
                    }
                }
            }
            return sql;
        }
        catch (Exception e){
            return sql;
        }
    }
}
